<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">

				<el-form-item label="省份" prop="province">
					<el-select v-model="filters.province" filterable :filter-method="selectfilter"  clearable placeholder="请选择省份">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in info"
								:key="item.provincename"
								:label="item.provincename"
								:value="item.provincename">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item label="地市" prop="province">
					<el-select v-model="filters.city" filterable clearable placeholder="请选择地市">
						<el-option value="0" label="全部">全部</el-option>
						<el-option
								v-for="item in citys"
								:key="item.city"
								:label="item.city"
								:value="item.city">
						</el-option>
					</el-select>
				</el-form-item>

				<el-form-item>
					<el-input v-model="filters.phoneNum" clearable placeholder="充值号码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.operators" clearable placeholder="运营商"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.wxOrder" clearable placeholder="微信单号"></el-input>
				</el-form-item>

				<el-form-item>
					<el-input v-model="filters.price" clearable placeholder="面额"></el-input>
				</el-form-item>

				<el-form-item label="订单状态" prop="state">
					<el-select v-model="filters.rechargeState" clearable placeholder="订单状态">
						<el-option value="0" label="全部">全部</el-option>
						<el-option value="1" label="充值中">充值中</el-option>
						<el-option value="2" label="充值成功">充值成功</el-option>
						<el-option value="3" label="充值失败">充值失败</el-option>
						<el-option value="4" label="充值退单">充值退单</el-option>
						<el-option value="5" label="订单提交">订单提交</el-option>
						<el-option value="6" label="已撤销">已撤销</el-option>
						<el-option value="7" label="被合单">被合单</el-option>
						<el-option value="8" label="被拆">被拆</el-option>
						<!--充值状态 0 删除 1充值中 2充值成功 3充值失败 4 充值退单 5订单提交 6已撤销 7 被合单 8 被拆单-->
					</el-select>
				</el-form-item>
				<el-form-item label="充值状态" prop="state">
					<el-select v-model="filters.payState" clearable placeholder="充值状态">
						<el-option value="0" label="全部">全部</el-option>
						<el-option value="1" label="支付中">支付中</el-option>
						<el-option value="2" label="支付成功">支付成功</el-option>
						<el-option value="3" label="支付失败">支付失败</el-option>
						<!--微信支付状态 1支付中 2支付成功 3支付失败-->
					</el-select>
				</el-form-item>

				<el-form-item label="支付类型" prop="state">
					<el-select v-model="filters.isLock" clearable placeholder="支付类型(支付宝/微信)">
						<el-option value="-1" label="全部">全部</el-option>
						<el-option value="alipay" label="支付宝">支付宝</el-option>
						<el-option value="wechat" label="微信">微信</el-option>
					</el-select>
				</el-form-item>

<!--				<el-form-item label="锁定状态" prop="state">-->
<!--					<el-select v-model="filters.isLock" placeholder="锁定状态">-->
<!--						<el-option value="-1" label="全部">全部</el-option>-->
<!--						<el-option value="1" label="锁定">锁定</el-option>-->
<!--						<el-option value="0" label="正常">正常</el-option>-->
<!--					</el-select>-->
<!--				</el-form-item>-->

				<el-form-item>
					<el-date-picker type="datetime" clearable placeholder="开始时间"  format="yyyy-MM-dd HH:mm:ss"   v-model="filters.importTime" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="datetime" clearable placeholder="结束时间" format="yyyy-MM-dd HH:mm:ss"   v-model="filters.endtime" style="width: 100%;"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="findPageSlowOrder">查询</el-button>
				</el-form-item>

				<!--<el-form-item>-->
					<!--<el-button type="primary" v-on:click="importBroadband">导入</el-button>-->
				<!--</el-form-item>-->

				<el-form-item>
					<el-button type="primary" v-show="isShowSupper" v-on:click="exportSlowOrder">导出</el-button>
				</el-form-item>
<!--				<el-form-item>-->
<!--					<el-button type="primary" v-on:click="importSlowOrder">导入</el-button>-->
<!--				</el-form-item>-->

			</el-form>
		</el-col>
		<!--列表-->
		<template>
			<el-table :data="sloworders" highlight-current-row v-loading="loading" max-height="620" @selection-change="selsChange" border  style="width: 100%;">
				<el-table-column  type="selection"  width="50" label="编号">
				</el-table-column>
				<el-table-column prop="outTradeNo" label="系统单号" width="130px" show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="phoneNum" show-overflow-tooltip width="130px" label="手机号" show-overflow-tooltip >
				</el-table-column>
				<el-table-column prop="payType" label="支付类型" show-overflow-tooltip  width="110px"  >
				</el-table-column>
				<el-table-column prop="operators" label="运营商" show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="province" label="省份"  show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="city" label="地市" show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="price" label="面额"  show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="wxOrder" label="支付单号"  width="260px"   show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="wxShopId" label="商户号"   width="120px" show-overflow-tooltip  >
				</el-table-column>
				<el-table-column prop="discount" label="折扣" show-overflow-tooltip   >
				</el-table-column>
				<el-table-column prop="payPrice" label="实付" show-overflow-tooltip   >
				</el-table-column>
				<el-table-column prop="rechargeState" label="状态" show-overflow-tooltip  width="100px"  :formatter="formatState" >
				</el-table-column>
				<el-table-column prop="payState" label="支付状态" show-overflow-tooltip  width="100px"   :formatter="formatPayState"  >
				</el-table-column>
				<el-table-column prop="cashFee" label="支付金额" show-overflow-tooltip  width="100px"  :formatter="formatcashFee"  >
				</el-table-column>
				<el-table-column prop="commitTime" label="提交充值时间"   width="170px" show-overflow-tooltip >
				</el-table-column>
				<el-table-column prop="endTime" label="结束时间"   width="170px" show-overflow-tooltip   >
				</el-table-column>
				<el-table-column prop="remark" label="备注"  show-overflow-tooltip   >
				</el-table-column>

				<el-table-column label="操作"  fixed="right"   width="280">
					<template scope="scope"   >
						<el-button size="small"   @click="recharegeSuccess(scope.$index, scope.row)">充值成功
						</el-button>
						<el-button size="small"   @click="recharegeFailt(scope.$index, scope.row)">充值失败
						</el-button>
<!--						<el-button size="small"   @click="deleteSlowOrder(scope.$index, scope.row)">-->
<!--						删除-->
<!--						</el-button>-->
					</template>
				</el-table-column>
			</el-table>
		</template>
		<el-pagination
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
				layout="total,sizes,prev, pager, next"
				:total="total">
		</el-pagination>

		<el-dialog title="导入订单处理结果" v-model="importFormVisible" :close-on-click-modal="false">
			<el-form :model="importForm" label-width="200px"  :inline="true" :rules="importFormRules"  ref="broadband">
				<!--http://119.23.127.152:8283/weiliu/sloworder/importSlowOrder http://localhost:9899/weiliu/weixinorder/importSlowOrder   http://119.23.127.152:8283/weiliu/weixinorder/importSlowOrder-->
				<el-form-item label="选择文件">
					<el-upload
							ref="importBroadbandUpload"
							action="http://119.23.127.152:8283/weiliu/weixinorder/importSlowOrder"
							:limit="1"
							multiple
							:file-list="fileList"
							:beforeUpload="beforeAvatarUpload"
							:on-exceed="handleExceed"
							:on-success="handleSuccess"
							:on-preview="handlePictureCardPreview"
							:auto-upload="false"
							:on-remove="handleRemove">
						<el-button slot="trigger" size="small" type="primary" :inline="true" >选择文件</el-button>
						<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload" :inline="true">上传到服务器</el-button>
						<div slot="tip" class="el-upload__tip">只能上传excel文件,并且单次只能上传一个文件</div>
					</el-upload>

				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="importFormVisible=false">取消</el-button>

				<!--<el-button type="primary" @click.native="importFormSubmit" :loading="importFormLoading">提交</el-button>-->
			</div>
		</el-dialog>
	</section>

</template>
<script>
	import { findPageSlowOrder ,findAllPro,findProvinceCity,exportWxOrder,updateOrderState} from '../../api/api';



    //import NProgress from 'nprogress'
    import moment from 'moment';
    var crypto = require('crypto');
	export default {
		data() {
			return {
				filters: {
					importTime:  moment(new Date(),"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD') + " 00:00:00",
                    province:'',
					price:'',
					rechargeState:'',
					phoneNum:'',
                    city:'',
                    isLock:'',
                    operators:'',
                    wxOrder:'',
                    endtime:'',
					payState:''
				},
                fileList: [],
                isShowSupper:JSON.parse(sessionStorage.getItem("user")).usertype === 2 ? true : false,
				start:0,
				pageSize:10,
                total:0,
				loading: false,
				users: [],
                info:[],
                citys:[],
                sloworders:[],
                sels:[],
                importFormVisible: false,
                importFormLoading: false,
                importFormRules: {
                },
                importForm: {

                },
			}
		},
		methods: {

			formatLock: function (row, column) {
                //1 管理员 2超级管理员 3 下游渠道商
				return row.isLock == 1 ? '锁定' : row.isLock == 0 ? '正常' : "其他";
			},
            formatState: function (row, column) {
//              充值状态 0 删除 1充值中 2充值成功 3充值失败 4 充值退单 5订单提交 6已撤销 7 被合单 8 被拆单
                return row.rechargeState == 1 ? '充值中' : row.rechargeState == 2 ? ' 充值成功 ' :  row.rechargeState == 3 ? '充值失败' : row.rechargeState == 5 ? '订单提交'
						: row.rechargeState == 4 ? '充值退单 ' :  row.rechargeState == 6 ? '已撤销 ' :  row.rechargeState == 7 ? '被合单 ' :   row.rechargeState == 8 ? '被拆单 ' : "未知";
            },formatPayState: function (row, column) {
                //微信支付状态 1支付中 2支付成功 3支付失败
                return row.payState == 1 ? '支付中' : row.payState == 2 ? '支付成功' :  row.payState == 3 ? "支付失败" : "未知";
            },formatcashFee: function (row, column) {
                //单位分
                return row.cashFee / 100  + " 元";
            },
            handleAdd:function(){
				this.addFormVisible = true;
			},

			findPageSlowOrder: function () {
				let para = {
					start:this.start,
					pageSize:this.pageSize,
					importTime:this.filters.importTime == undefined ? '' :   moment(this.filters.importTime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
                    endtime: this.filters.endtime == '' ? '' :  moment(this.filters.endtime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
                    province: this.filters.province == "0" ? "" :this.filters.province,
					rechargeState:this.filters.rechargeState == "0" ? "" : this.filters.rechargeState,
					payState:this.filters.payState == "0" ? "" : this.filters.payState,
					phoneNum:this.filters.phoneNum,
					price:this.filters.price,
                    city : this.filters.province == "0" ? "" : this.filters.city == "0" ? "" : this.filters.city,
                    isLock: this.filters.isLock == "-1" ? "" : this.filters.isLock,
                    wxOrder: this.filters.wxOrder ,
                    operators : this.filters.operators
				};
				this.loading = true;
                findPageSlowOrder(para).then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.sloworders = data.result;
                        this.total = data.totalCount;
                    }
                }).catch(e =>{

				});
			},handleCurrentChange:function(val){
                this.start = val - 1;
                this.findPageSlowOrder();
			},
            handleSizeChange:function(val){
			    this.pageSize = val;
			    this.findPageSlowOrder();
			} ,selsChange: function (sels) {
                this.sels = sels;
            } ,importBroadband:function () {
                this.importFormVisible = true;
            } ,
            findAllPro:function(){
                let para = {useruuid: ''};
                findAllPro(para).then(datas => {
                    let {message, code, data} = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.info = data;
                    }
                });
            },
            selectfilter:function(row){
                if(row != ""){
                    this.findProvinceCity(row)
                }
            },findProvinceCity:function (province) {
                let para = {
                    province : province
                };
                findProvinceCity(para).then((datas) => {
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    }
                    this.citys = data;
                });
            },deleteSlowOrder:function (index, row){
                this.$confirm('确认删除订单吗？订单电话号码:'+row.phoneNum, '提示', {}).then(() => {
                    let para = {
                        orderuuid: row.orderuuid
                    };
                    deleteSlowOrder(para).then((datas) => {
                        this.listLoading = false;
                        let {message, code, data} = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                        this.$message({
                            message: message,
                            type: 'info'
                        });
                        this.findPageSlowOrder();
                    });
                });
			},exportSlowOrder:function () {
                let para = {
                    importTime:this.filters.importTime == undefined ? '' :   moment(this.filters.importTime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
                    endtime: this.filters.endtime == '' ? '' :  moment(this.filters.endtime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
                    province: this.filters.province == "0" ? "" :this.filters.province,
                    rechargeState:this.filters.rechargeState == "0" ? "" : this.filters.rechargeState,
					payState:this.filters.payState == "0" ? "" : this.filters.payState,
                    phoneNum:this.filters.phoneNum,
                    price:this.filters.price,
                    city : this.filters.province == "0" ? "" : this.filters.city == "0" ? "" : this.filters.city,
					isLock: this.filters.isLock == "-1" ? "" : this.filters.isLock,
                    wxOrder: this.filters.wxOrder ,
                    operators : this.filters.operators
                };
                this.loading = true;
                exportWxOrder(para).then(datas => {
                    this.loading = false;
                    let { message, code, data } = datas;
                    if (code !== "0") {
                        this.$message({
                            message: message,
                            type: 'error'
                        });
                    } else {
                        this.findPageSlowOrder();
                        window.open(data.path);
                    }
                })
            },importSlowOrder:function () {
                this.importFormVisible = true;
            },handleRemove:function(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview:function(file) {
                console.log(file);
            },
            handleExceed:function(files, fileList) {
                if(fileList.length > 1){
                    this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
                }
            },
            beforeRemove:function(file, fileList) {
                return this.$confirm(`确定移除 ${ file.name }？`);
            },handlePictureCardPreview:function(file){
                console.log(file);
            },handleSuccess:function(response, file, fileList){
                this.findPageSlowOrder();
                this.fileList = [];
                if(response.code == "0"){
                    this.importFormVisible = false;
                    this.$message({
                        message: response.message,
                        type: 'info'
                    });
                }else{
                    this.$message({
                        message: response.message,
                        type: 'warning'
                    });
                }
            },submitUpload(){
                this.$refs.importBroadbandUpload.submit();
            },beforeAvatarUpload(file){
                var testmsg=file.name.substring(file.name.lastIndexOf('.')+1)
                const extension = testmsg === 'xls';
                const extension2 = testmsg === 'xlsx';
//                const isLt2M = file.size / 1024 / 1024 < 10;
                if(!extension && !extension2) {
                    this.$message({
                        message: '上传文件只能是 xls、xlsx格式!',
                        type: 'warning'
                    });
                }

                return extension || extension2 ;
            },recharegeSuccess(index,row){
                this.$confirm('确认手工操作成功订单吗？', '提示', {}).then(() => {
                    let para = {
                        orderuuid: row.orderuuid,
                        rechargeState:2
                    };
                    updateOrderState(para).then((datas) => {
                        this.listLoading = false;
                        let {message, code, data} = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                        this.$message({
                            message: message,
                            type: 'info'
                        });
                        this.findPageSlowOrder();
                    });
                });
			},recharegeFailt(index,row){
                this.$confirm('确认手工操作失败订单吗？', '提示', {}).then(() => {
                    let para = {
                        orderuuid: row.orderuuid,
                        rechargeState:3
                    };
                    updateOrderState(para).then((datas) => {
                        this.listLoading = false;
                        let {message, code, data} = datas;
                        if (code !== "0") {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                        this.$message({
                            message: message,
                            type: 'info'
                        });
                        this.findPageSlowOrder();
                    });
                });
			}
        },

		mounted() {
		    this.findAllPro();
			this.findPageSlowOrder();
		},
    	beforeCreate: function (){

		}
	};

</script>

<style scoped>

</style>
